package com.tnxst.phexamination.activity;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.SavedStateViewModelFactory;
import androidx.lifecycle.ViewModelProvider;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.tnxst.phexamination.R;
import com.tnxst.phexamination.callback.AppCallBack;
import com.tnxst.phexamination.constant.Constant;
import com.tnxst.phexamination.constant.HISConfig;
import com.tnxst.phexamination.databinding.ActivityLoginBinding;
import com.tnxst.phexamination.dto.DoctorDTO;
import com.tnxst.phexamination.dto.ServicePackageDTO;
import com.tnxst.phexamination.public_health_docking_interface.FamilySignInterface;
import com.tnxst.phexamination.public_health_docking_interface.SyncInterface;
import com.tnxst.phexamination.public_health_docking_interface.SystemInterface;
import com.tnxst.phexamination.room.dao.ServicePackageDao;
import com.tnxst.phexamination.room.repository.RoomRepository;
import com.tnxst.phexamination.room.repository.ServicePackageRepository;
import com.tnxst.phexamination.utils.AddressUtils;
import com.tnxst.phexamination.utils.GsonUtils;
import com.tnxst.phexamination.utils.ReflectUtils;
import com.tnxst.phexamination.utils.RequestUtils;
import com.tnxst.phexamination.utils.StringUtils;
import com.tnxst.phexamination.utils.Utils;
import com.tnxst.phexamination.utils.WebServiceUtils;
import com.tnxst.phexamination.utils.WebUtils;
import com.tnxst.phexamination.viewmodel.GlobalViewModel;
import com.tnxst.phexamination.vo.LoginVo;
import com.tnxst.phexamination.vo.TeamVo;
import com.tnxst.phexamination.vo.UserInfoVo;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import okhttp3.Call;

public class LoginActivity extends BaseActivity {

    private GlobalViewModel viewModel;

    private ActivityLoginBinding binding;

    private AlertDialog show;

    private Context context;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        viewModel = new ViewModelProvider(this, new SavedStateViewModelFactory(getApplication(), this)).get(GlobalViewModel.class);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_login);
        binding.setLogin(viewModel);

        binding.setLifecycleOwner(this);
        context = this;
        initView();
        viewModel.getAddressUtils().init(this);
    }
    
    private void initView() {

        binding.editText.setText(viewModel.getSharedPreferences().getString(Constant.ACCOUNT,""));
        if (viewModel.getSharedPreferences().getBoolean(Constant.REMEMBER_PASSWORD,false)){
            binding.editText2.setText(viewModel.getSharedPreferences().getString(Constant.PASSWORD,""));
            binding.checkBox.setChecked(true);
        }
        binding.textView2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String account = binding.editText.getText().toString();
                String password = binding.editText2.getText().toString();
                if (!account.isEmpty() && !password.isEmpty()){
//                    if (account.equals(viewModel.getSharedPreferences().getString(Constant.ACCOUNT,""))
//                            && password.equals(viewModel.getSharedPreferences().getString(Constant.PASSWORD,""))){
//                        viewModel.setUserInfo(GsonUtils.fromJson(viewModel.getSharedPreferences().getString(Constant.LOGIN_RESULT,""), DoctorDTO.class));
//                        Intent intent = new Intent(LoginActivity.this, IndexActivity.class);
//                        startActivity(intent);
//                        finish();
//                        return;
//                    }

                    if (account.equals("admin")
                            && password.equals("admin")){
                        DoctorDTO doctorDTO = new DoctorDTO();
                        doctorDTO.setDoctorName("管理员");
                        doctorDTO.setDoctorPhone("13695546996");
                        doctorDTO.setDId("0");
                        doctorDTO.setOrganizationName("天南星科技有限公司");
                        doctorDTO.setLoginCode("admin");
                        doctorDTO.setLoginPassword("admin");
                        doctorDTO.setDoctorIdentity("管理员");
                        doctorDTO.setDoctorAddress("成都市");
                        viewModel.setUserInfo(doctorDTO);
                        Intent intent = new Intent(LoginActivity.this, IndexActivity.class);
                        startActivity(intent);
                        finish();
                        return;
                    }

                    //登录验证
                    DoctorDTO doctorDTO = viewModel.getDoctorRepository().findByField("loginCode", account);
                    if (doctorDTO == null || !password.equals(doctorDTO.getLoginPassword())){
                        Utils.Toast(LoginActivity.this, "账号或密码错误！");
                        return;
                    }

                    final AlertDialog.Builder alertDialog = new AlertDialog.Builder(context, R.style.AlertDialog);
                    View view = View.inflate(context, R.layout.login_loading, null);
                    alertDialog
                            .setView(view)
                            .create();
                    show = alertDialog.show();
                    show.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
                    show.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
                    TextView textView = show.findViewById(R.id.imageView1);
                    AnimationDrawable ad = (AnimationDrawable) textView.getCompoundDrawables()[1];
                    ad.start();




                    //登录成功
                    SharedPreferences.Editor edit = viewModel.getSharedPreferences().edit();
                    //储存账号密码登常用信息
                    edit.putString(Constant.ACCOUNT, account);
                    if (binding.checkBox.isChecked()){
                        edit.putString(Constant.PASSWORD,password);
                        edit.putBoolean(Constant.REMEMBER_PASSWORD,true);
                    }else {
                        edit.putBoolean(Constant.REMEMBER_PASSWORD,false);
                        edit.putString(Constant.PASSWORD,password);
                    }
                    edit.apply();
                    //储存登录返回结果
                    viewModel.getSharedPreferences().edit().putString(Constant.LOGIN_RESULT, GsonUtils.toJson(doctorDTO)).apply();
                    show.dismiss();
                    ad.stop();
                    Intent intent = new Intent(LoginActivity.this, IndexActivity.class);
                    startActivity(intent);
                    finish();
                }else {
                    Utils.Toast(LoginActivity.this, "账号或密码不能为空！");
                }
            }
        });
    }

}
